package com.apikey.auth.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.Table;
import java.time.LocalDateTime;

/**
 * 访问日志实体类
 * 记录API Key的访问信息
 *
 * @author API Key Framework
 * @version 1.0
 */
@Entity
@Table(name = "access_logs", indexes = {
        @Index(name = "idx_api_key_value", columnList = "api_key_value"),
        @Index(name = "idx_client_ip", columnList = "client_ip"),
        @Index(name = "idx_access_time", columnList = "access_time"),
        @Index(name = "idx_response_status", columnList = "response_status")
})
public class AccessLog {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "api_key_value", nullable = true, length = 64)
    private String apiKeyValue;

    @Column(name = "request_path", nullable = false, length = 500)
    private String requestPath;

    @Column(name = "request_method", nullable = false, length = 10)
    private String requestMethod;

    @Column(name = "request_headers", columnDefinition = "TEXT")
    private String requestHeaders;

    @Column(name = "request_params", columnDefinition = "TEXT")
    private String requestParams;

    @Column(name = "client_ip", nullable = false, length = 45)
    private String clientIp;

    @Column(name = "user_agent", length = 500)
    private String userAgent;

    @Column(name = "response_status", nullable = false)
    private Integer responseStatus;

    @Column(name = "response_headers", columnDefinition = "TEXT")
    private String responseHeaders;

    @Column(name = "access_time", nullable = false)
    private LocalDateTime accessTime;

    @Column(name = "response_time", nullable = false)
    private Long responseTime;

    @Column(name = "api_key_id")
    private Long apiKeyId;

    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;

    /**
     * 默认构造函数
     */
    public AccessLog() {
        this.accessTime = LocalDateTime.now();
    }

    /**
     * 构造函数
     *
     * @param apiKeyValue   API Key值
     * @param requestPath   请求路径
     * @param requestMethod 请求方法
     * @param clientIp      客户端IP
     */
    public AccessLog(String apiKeyValue, String requestPath, String requestMethod, String clientIp) {
        this();
        this.apiKeyValue = apiKeyValue;
        this.requestPath = requestPath;
        this.requestMethod = requestMethod;
        this.clientIp = clientIp;
    }

    // Getter和Setter方法

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getApiKeyValue() {
        return apiKeyValue;
    }

    public void setApiKeyValue(String apiKeyValue) {
        this.apiKeyValue = apiKeyValue;
    }

    public String getRequestPath() {
        return requestPath;
    }

    public void setRequestPath(String requestPath) {
        this.requestPath = requestPath;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
    }

    public String getRequestHeaders() {
        return requestHeaders;
    }

    public void setRequestHeaders(String requestHeaders) {
        this.requestHeaders = requestHeaders;
    }

    public String getRequestParams() {
        return requestParams;
    }

    public void setRequestParams(String requestParams) {
        this.requestParams = requestParams;
    }

    public String getClientIp() {
        return clientIp;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }

    public String getUserAgent() {
        return userAgent;
    }

    public void setUserAgent(String userAgent) {
        this.userAgent = userAgent;
    }

    public Integer getResponseStatus() {
        return responseStatus;
    }

    public void setResponseStatus(Integer responseStatus) {
        this.responseStatus = responseStatus;
    }

    public String getResponseHeaders() {
        return responseHeaders;
    }

    public void setResponseHeaders(String responseHeaders) {
        this.responseHeaders = responseHeaders;
    }

    public LocalDateTime getAccessTime() {
        return accessTime;
    }

    public void setAccessTime(LocalDateTime accessTime) {
        this.accessTime = accessTime;
    }

    public Long getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(Long responseTime) {
        this.responseTime = responseTime;
    }

    public Long getApiKeyId() {
        return apiKeyId;
    }

    public void setApiKeyId(Long apiKeyId) {
        this.apiKeyId = apiKeyId;
    }

    public Boolean getIsDeleted() {
        return isDeleted;
    }

    public void setIsDeleted(Boolean isDeleted) {
        this.isDeleted = isDeleted;
    }

    /**
     * 检查访问是否成功
     *
     * @return 是否成功
     */
    public boolean isSuccessful() {
        return responseStatus != null && responseStatus >= 200 && responseStatus < 300;
    }

    /**
     * 检查是否为客户端错误
     *
     * @return 是否为客户端错误
     */
    public boolean isClientError() {
        return responseStatus != null && responseStatus >= 400 && responseStatus < 500;
    }

    /**
     * 检查是否为服务器错误
     *
     * @return 是否为服务器错误
     */
    public boolean isServerError() {
        return responseStatus != null && responseStatus >= 500;
    }

    /**
     * 获取响应时间描述
     *
     * @return 响应时间描述
     */
    public String getResponseTimeDescription() {
        if (responseTime == null) {
            return "未知";
        }

        if (responseTime < 100) {
            return "快速";
        } else if (responseTime < 500) {
            return "正常";
        } else if (responseTime < 1000) {
            return "较慢";
        } else {
            return "缓慢";
        }
    }

    @Override
    public String toString() {
        return "AccessLog{" +
                "id=" + id +
                ", apiKeyValue='" + apiKeyValue + '\'' +
                ", requestPath='" + requestPath + '\'' +
                ", requestMethod='" + requestMethod + '\'' +
                ", clientIp='" + clientIp + '\'' +
                ", responseStatus=" + responseStatus +
                ", accessTime=" + accessTime +
                ", responseTime=" + responseTime +
                '}';
    }
}